<template>
	<view>
		<uni-swiper-dot :info="info" :current="current" field="content" :mode="mode">
			<swiper class="" @change="change" style="height: 240px;">
				<swiper-item v-for="(item ,index) in info" :key="index">
					<view class="">
						<image :src="item" mode='' style="width: 100%;"></image>
					</view>
				</swiper-item>
			</swiper>
		</uni-swiper-dot>
		<view class="bl" style="padding-left: 20px;padding-right: 20px;">
			<view class="bc">
				<i class="ri-service-fill" style="color:#008040;font-size:30px;"></i>
				<!-- <i-icon size="30px" color="#008040" name="service-fill"></i-icon> -->
				<view class="h1">公益服务</view>
			</view>
			<view class="bc">
				<i class="ri-map-pin-line" style="color:#008040;font-size:16px;"></i>
				<!-- <i-icon size="16px" color="#008040" name="map-pin-line"></i-icon> -->
				<text>{{sitationName}}</text>
			</view>
		</view>
		<view class="p-fuwu" v-for="(item,index) in publicServiceList">
			<view>
				<!-- 没有免费日的不显示 -->
			</view>
			<view class="" style="height: 95px;">
				<view style="width: 80px;position: absolute;">
					<!-- <image class="p-fuwu-img" :src="'data:image/jpeg;base64,'+item.imgPath"  mode="aspectFill"
						v-if="item.imgPath != null">
					</image> -->
					<image class="p-fuwu-img" :src="'https://admin.yunshuicha.com'+item.imgPath" mode="aspectFill"
						v-if="item.imgPath != null">
					</image>
					<image class="p-fuwu-img" src="../../static/f.jpg" mode="aspectFill" v-else></image>
				</view>
				<view style="margin-left:95px; height: 80px;">
					<view class="bl" style="padding-top: 10px;">
						<view class="h2">{{item.serve}}</view>
						<button class="b-middle" @click="toyuyue(item)">预约</button>
					</view>
					<view class="f14"><text class="gre r10">{{item.freeDate}}</text>免费</view>
				</view>
			</view>
		</view>
		<view style="background-color: #E7E7E7;height: 15px;"></view>
		<view class="bc" style="padding-left: 20px;padding-right: 20px;">
			<i class="ri-swap-fill" style="color:#008040;font-size:30px;"></i>
			<!-- <i-icon size="30px" color="#008040" name="swap-fill"></i-icon> -->
			<view class="h1">废旧回收</view>
		</view>
		<view class="bl" style="padding: 10px 30px 30px 30px;">
			<view class="" @click="toFeijiu">
				<view class="tc">
					<i class="ri-money-dollar-circle-fill" style="color:#008040;font-size:42px;"></i>
					<!-- <i-icon size="42px" color="#008040" name="money-dollar-circle-fill"></i-icon> -->
				</view>
				<view class=""><text>我卖废旧</text></view>
			</view>
			<view class="" @click="toaixin()">
				<view class="tc">
					<i class="ri-hand-heart-fill" style="color:#008040;font-size:42px;"></i>
					<!-- <i-icon size="42px" color="#008040" name="hand-heart-fill"></i-icon> -->
				</view>
				<view class=""><text>爱心兑换</text></view>
			</view>
			<view class="" @click="toCenter">
				<view class="tc">
					<i class="ri-user-fill" style="color:#008040;font-size:42px;"></i>
					<!-- <i-icon size="42px" color="#008040" name="user-3-fill" @click="toCenter"></i-icon> -->
				</view>
				<view class=""><text>个人中心</text></view>
			</view>
		</view>
		<view style="background-color: #E7E7E7;height: 15px;"></view>
		<view style="font-size: 12px;text-align: center;padding: 20px 0px 20px 0px;">
			技术支持：广西奇果网络科技有限公司
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '桂民社区服务站',
				sitationName: '华润幸福里服务站',
				serviceSitationList: [],
				slist: false,
				publicServiceList: [],
				siteId: '',
				thisDate: '',
				info: [
					"../../static/01.jpg",
					"../../static/02.jpg",
					"../../static/03.jpg"
				],
				current: 0,
				mode: 'round',
				wxType: 0,
				xzData:{}
			}
		},
		methods: {
			GetRequest(value) {
				// console.log(value)
				if (value.indexOf("?") != -1) { //url中存在问号，也就说有参数。
					let indx = value.indexOf("?");
					var str = value.substr(indx + 4); //得到?后面的字符串
					// let duoyu = str.indexOf("#");
					// var trueid = str.substring(0, duoyu);
				}
				this.siteId = str;
				console.log(this.siteId)
			},
			change(e) {
				this.current = e.detail.current;
			},
			toFeijiu: function() {
				uni.navigateTo({
					url: 'fw-feijiu?stationName=' + this.sitationName
				})
			},
			/**
			 * 去预约
			 * @param {Object} item 服务
			 */
			toyuyue: function(service) {
				let _this = this;
				uni.setStorage({
				    key: 'serviceId',
				    data: service.id
				});
				uni.setStorage({
				    key: 'serviceName',
				    data: service.serve
				});
				uni.setStorage({
				    key: 'serviceDate',
				    data: service.sitePrice
				});
				uni.setStorage({
				    key: 'serviceSite',
				    data: service.villageAddress
				});
				uni.showModal({
					title: '服务预约',
					cancelText: '收费',
					confirmText: '免费',
					content: '请选择服务收费类型，免费只能在免费日当天可用',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击免费');
							uni.navigateTo({
								url: 'fw-yuyue?serviceId=' + service.id +
									'&serviceName=' + service.serve +
									'&serviceDate=' + service.freeDate +
									'&serviceSite=' + service.villageAddress +
									'&price=0' +
									'&unit=元'
							})
						} else if (res.cancel) {
							console.log('用户点击收费');

							if (_this.wxType == 1) {
								let rUrl ='http%3A%2F%2Fh5.yunshuicha.com%2F';
								let oauthUrl =
									'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxfdb3b7e5fb01d447&redirect_uri=' +
									rUrl +
									'&response_type=code&scope=snsapi_base&state=123#wechat_redirect';
								window.location.href = oauthUrl; //打开微信授权链接,自动授权后会重定向到预约页面
							} else if (_this.wxType == 2) {
								uni.navigateTo({
									url: 'fw-yuyue-cost?serviceId=' + service.id +
										'&serviceName=' + service.serve +
										'&serviceDate=' + service.sitePrice +
										'&serviceSite=' + service.villageAddress
								})
							} else {
								uni.navigateTo({
									url: 'fw-yuyue-cost?serviceId=' + service.id +
										'&serviceName=' + service.serve +
										'&serviceDate=' + service.sitePrice +
										'&serviceSite=' + service.villageAddress
								})
							}
						}
					}
				});
			},
			//获取路径上的code参数
			getUrlParam(name) {
				var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
				let url = window.location.href.split('#')[0]
				let search = url.split('?')[1]
				if (search) {
					var r = search.substr(0).match(reg)
					if (r !== null)
						return unescape(r[2])
					return null
				} else
					return null
			},
			//判断是否是微信内部浏览器
			is_weixin() {
				let _this = this;
				var ua = navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == "micromessenger") {
					console.log('是微信内部浏览器')
					_this.wxType = 1;
					return true;
				} else {
					_this.wxType = 2;
					console.log('是外部浏览器')
					return false;
				}
			},
			toaixin: function() {
				uni.navigateTo({
					url: 'fw-aixin'
				})
			},
			//个人中心
			toCenter: function() {
				uni.navigateTo({
					url: '../u/u-center'
				})
			},
			/**
			 * 获取格式化的今日时间
			 */
			getToDayDate() {
				let date = new Date();
				let year = date.getFullYear();
				let day = date.getDay();
				if (day < 10) {
					day = "0" + day;
				}
				let month = date.getMonth() + 1;
				if (month < 10) {
					month = "0" + month;
				}
				this.thisDate = year + '-' + month + '-' + day
				console.log(this.thisDate)
			},

			// 获取公益服务
			getPublicServiceList() {
				let _this = this
				this.httpRequest({
					url: '/guiminSite/list/cg',
					method: 'POST',
					data: {
						villageName: _this.sitationName
					},
					fn(data) {
						uni.hideLoading()
						_this.serviceSitationList = data
						_this.checkedStation({
							villageName: _this.sitationName
						})
					}
				})
			},
			// 据站点名称（villageName）获取对应站点的公益服务
			checkedStation(item) {
				this.sitationName = item.villageName
				this.publicServiceList = []
				for (let i = 0; i < this.serviceSitationList.length; i++) {
					let one = this.serviceSitationList[i]
					if (one.villageName === item.villageName && one.serve) {
						this.publicServiceList.push(one)
					}
				}
				this.slist = false
			},
			getUserINfo(){
				let _this = this
				uni.showLoading()
				uni.request({
					url: this.baseURL + '/get/user/info',
					method: 'POST',
					header: {
						token: uni.getStorageSync('token')
					},
					success(res) {
						let data = res.data
						if (data.code == 666) {
							uni.reLaunch({
								url: 'login'
							})
							return
						}
						if (data.success == true) {
				
							let datas = data.data
							uni.setStorage({
								key: 'stationId',
								data: datas.id
							})
							uni.setStorage({
								key: 'stationName',
								data: datas.villageName
							})
							_this.sitationName = datas.villageName
							console.error(_this.sitationName)
							_this.getPublicServiceList()
				
							_this.httpRequest({
								url: "/my/info",
								method: "GET",
								fn(data) {
									console.log(data)
									uni.setStorage({
										key: 'uid',
										data: data.id
									})
								}
							})
				
						} else {
							uni.hideLoading()
							uni.showToast({
								title: data.message,
								icon: 'error',
								duration: 3000
							})
							uni.redirectTo({
								url: 'login?id=' + this.siteId
							})
						}
					},
					fail: (err) => {
						uni.hideLoading()
						console.log(err)
						uni.redirectTo({
							url: 'login?id=' + this.siteId
						})
					}
				})
			}
		},
		onLoad() {
			this.is_weixin();
			this.GetRequest(location.href);
			if (!uni.getStorageSync('token')) {
				uni.redirectTo({
					url: 'login?id=' + this.siteId
				})
				return
			}
			this.getUserINfo();
			let wxCode = this.getUrlParam('code');
			if(wxCode){
				let rID = uni.getStorageSync('serviceId');
				let rName = uni.getStorageSync('serviceName');
				let rData = uni.getStorageSync('serviceDate');
				let rSite = uni.getStorageSync('serviceSite');
				
				uni.navigateTo({
					url: 'fw-yuyue-cost?serviceId=' + rID +
						'&serviceName=' + rName +
						'&serviceDate=' + rData +
						'&serviceSite=' + rSite +
						'&code='+wxCode
				})
			}
		}
	}
</script>
